unit ufrmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB,
  StdCtrls, ExtCtrls, Grids,
  DBGrids, DBClient;

type
  TfrmMain = class(TForm)
    dsMain: TDataSource;
    cdsMain: TClientDataSet;
    dbgrdMain: TDBGrid;
    pnlOperator: TPanel;
    btnOpen: TButton;
    btnApplyUpdate: TButton;
    btnDoAction: TButton;
    dbgrdDetails: TDBGrid;
    cdsDetails: TClientDataSet;
    dsDetails: TDataSource;
    procedure btnApplyUpdateClick(Sender: TObject);
    procedure btnDoActionClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure cdsMainAfterInsert(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses
  udmMain, uMyStringTools, uJSonStreamTools;

{$R *.dfm}

procedure TfrmMain.btnApplyUpdateClick(Sender: TObject);
begin
  dmMain.ApplyExecuter.clear;
  dmMain.ApplyExecuter.packageDataSetForUpdate(self.cdsMain, 'Products', 'ProductID');
  dmMain.ApplyExecuter.packageDataSetForUpdate(self.cdsDetails, '[Order Details]', 'OrderID;ProductID');
  dmMain.ApplyExecuter.execute;
end;

procedure TfrmMain.btnDoActionClick(Sender: TObject);
begin
  dmMain.QueryExecuter.Scripter.setScriptID('order.list');
  dmMain.QueryExecuter.Scripter.setParamsValue(':productid', self.cdsMain.FieldByName('ProductID').AsString);
  dmMain.QueryExecuter.queryCDS(cdsDetails);

//
//  dmMain.IOCPConnection.sendObject.Clear();
//  dmMain.IOCPConnection.sendObject.Json.S['cmd.namespace'] := 'custom';
//  dmMain.IOCPConnection.sendObject.Json.I['cmd.index'] := 1;
//  dmMain.IOCPConnection.sendObject.Json.S['params.productid'] := self.cdsMain.FieldByName('ProductID').AsString;
//  dmMain.IOCPConnection.Execute;
//  cdsDetails.XMLData := TJSonStreamTools.extractStringFromStream(dmMain.IOCPConnection.recvObject);
end;

procedure TfrmMain.btnOpenClick(Sender: TObject);
var
  i:Integer;
begin
  dmMain.QueryExecuter.Scripter.setScriptID('goods.list');
  dmMain.QueryExecuter.Scripter.setParamsValue('$rep_where$', ' and 2=2');
  dmMain.QueryExecuter.queryCDS(self.cdsMain);

  for i := 0  to self.dbgrdMain.Columns.Count -1 do
  begin
    dbgrdMain.Columns[i].Width := 100;
  end;

end;

procedure TfrmMain.cdsMainAfterInsert(DataSet: TDataSet);
begin
  self.cdsMain.FieldByName('FKey').AsString := TMyStringTools.createGUIDString;
  self.cdsMain.FieldByName('FUpbuildTime').AsDateTime := Now();
end;

end.
